package com.dc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.dc.dao.UsersDao;
import com.dc.entity.Users;
import com.dc.tools.DBTools;
import com.dc.tools.PageData;

/**
 * 实现UsersDao接口的方法
 * 
 * @author HCH
 *
 */
public class UsersDaoImpl implements UsersDao {

	/**
	 * 添加用户
	 */
	@Override
	public boolean addUsers(Users users) {
		// TODO Auto-generated method stub
		// 用户编号userid自动增加，用户状态userstate默认为true
		String sql = "insert into users values(null,?,?,?,?,?,true)";
		return DBTools.exUpdate(sql, users.getUsername(), users.getUserpsw(), users.getUserphone(),
				users.getUseremail(), users.getUseraddress()) > 0;
	}

	/**
	 * 根据用户编号userid删除用户
	 */
	@Override
	public boolean deleteUsers(int userid) {
		// TODO Auto-generated method stub
		String sql = "delete from users where userid = ?";
		return DBTools.exUpdate(sql, userid) > 0;
	}

	/**
	 * 根据用户编号userid修改用户信息
	 */
	@Override
	public boolean updateUsers(Users users) {
		// TODO Auto-generated method stub
		String sql = "update users set username=?,userphone=?,useremail=?,useraddress=?,userstate=? where userid=?";
		return DBTools.exUpdate(sql, users.getUsername(), users.getUserphone(), users.getUseremail(),
				users.getUseraddress(), users.isUserstate(), users.getUserid()) > 0;
	}

	/**
	 * 重置用户密码
	 */
	@Override
	public boolean resetUserpsw(int userid, String userpsw) {
		// TODO Auto-generated method stub
		String sql = "update users set userpsw=? where userid=?";
		return DBTools.exUpdate(sql, userpsw, userid) > 0;
	}

	/**
	 * 根据用户名username查询用户
	 */
	@Override
	public Users selectUsersByName(String username) {
		// TODO Auto-generated method stub
		String sql = "select*from users where username=?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, username);
		// 如果集合长度为0，则未找到对象
		if (list.size() == 0) {
			return null;
		}
		// 否则返回集合的第一个元素
		return list.get(0);
	}

	/**
	 * 根据关键词查找用户
	 */
	@Override
	public List<Users> selectUsersByKeyword(String keyword) {
		// TODO Auto-generated method stub
		String sql = "select*from users where username like ?";
		return (List<Users>) DBTools.exQuery(sql, Users.class, "%" + keyword + "%");
	}
	
	/**
	 * 根据手机号查找用户
	 */
	@Override
	public Users selectUsersByPhone(String userphone) {
		// TODO Auto-generated method stub
		String sql = "Select*from users where userphone=? ";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, userphone);
		// 如果集合长度为0，则未找到对象
		if (list.size() == 0) {
			return null;
		}
		// 否则返回集合的第一个元素
		return list.get(0);
	}

	/**
	 * 查找所有用户信息
	 */
	@Override
	public List<Users> selectUsers() {
		// TODO Auto-generated method stub
		String sql = "select*from users";
		return (List<Users>) DBTools.exQuery(sql, Users.class, null);
	}

	/**
	 * 分页显示用户信息
	 */
	@Override
	public PageData<Users> selectUsersByPage(int page, int pageSize, String keyword) {
		// TODO Auto-generated method stub
		PageData<Users> pd = DBTools.exQueryByPage("select*from users where username like ?", Users.class, page,
				pageSize, "%" + keyword + "%");
		return pd;
	}

	/**
	 * 登录用户操作的模拟
	 * 
	 * @param name
	 * @param psw
	 * @return true 登录成功 false 登录失败
	 */
	@Override
	public boolean doLogin(String name, String psw) {
		// 完成登录验证的sql语句
		String sql = "select count(userid) from users where username=? and userpsw = ?";
		// 得到连接对象
		Connection conn = DBTools.getConn();
		// 创建PreparedStatement对象
		PreparedStatement pst = null;
		ResultSet rs = null;
		int n = 0;

		try {
			pst = conn.prepareStatement(sql);
			// 设置？的值
			pst.setString(1, name);
			pst.setString(2, psw);
			// 执行sql
			rs = pst.executeQuery();
			// 处理结果
			if (rs.next()) {
				n = rs.getInt(1);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// 释放资源
			try {
				rs.close();
				pst.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return n == 1;
	}

	@Override
	public Users getUsersByNameAndPwd(String username, String userpsw) {
		// TODO Auto-generated method stub
		String sql = "select * from users where username = ? and userpsw= ?";
		List<Users> list = (List<Users>) DBTools.exQuery(sql, Users.class, username,userpsw);
		if (list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

}
